package yxy.game.pm2.module.stat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import yxy.cherry.game.base.mq.MqMessage;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.mq.*;
import yxy.game.pm2.bean.order.OrderInfo;

import java.util.Collection;
import java.util.Date;

public class StatHandle {

	static public void stat_资源增加(String playerCode, CCReason reason, Collection<Currency> currencies) {
		PlayerBean playerBean = PlayerBean.get(playerCode);
		if (playerBean == null || reason == null) {
			return;
		}
		MqMsg_CurrencyIncrease msg = new MqMsg_CurrencyIncrease();
		msg.setUid(playerBean.getUid());
		msg.setCode(playerBean.getCode());
		msg.setTime(new Date());
		msg.setServerid(playerBean.getServerId());
		
		msg.setReason(reason);

		for(Currency cur : currencies) {
			msg.setCurrency(cur);
			MqMessage.sendMessage(msg);
		}
		
	}

	static public void stat_资源减少(String playerCode, CCReason reason, Collection<Currency> currencies) {
		PlayerBean playerBean = PlayerBean.get(playerCode);
		if (playerBean == null || reason == null) {
			return;
		}
		MqMsg_CurrencyDecrease msg = new MqMsg_CurrencyDecrease();
		msg.setUid(playerBean.getUid());
		msg.setCode(playerBean.getCode());
		msg.setTime(new Date());
		msg.setServerid(playerBean.getServerId());
		
		msg.setReason(reason);

		for(Currency cur : currencies) {
			msg.setCurrency(cur);
			MqMessage.sendMessage(msg);
		}
	}

	static public void stat_等级变动(PlayerBean playerBean, int levelchage) {
		if (playerBean == null) {
			return;
		}
		MqMsg_LevelChange msg = new MqMsg_LevelChange();
		msg.setUid(playerBean.getUid());
		msg.setCode(playerBean.getCode());
		msg.setTime(new Date());

		msg.setServerid(playerBean.getServerId());
		msg.setLevel(playerBean.getLevel());
		msg.setLevelchange(levelchage);

		MqMessage.sendMessage(msg);
	}

	static public void stat_关卡统计(String playerCode, CCReason stageType, int stageId) {
		PlayerBean playerBean = PlayerBean.get(playerCode);
		if (playerBean == null || stageType == null) {
			return;
		}
		MqMsg_Stage msg = new MqMsg_Stage();
		msg.setUid(playerBean.getUid());
		msg.setCode(playerBean.getCode());
		msg.setTime(new Date());
		msg.setServerid(playerBean.getServerId());
		
		msg.setStageType(stageType);
		msg.setStageId(stageId);

		MqMessage.sendMessage(msg);
	}

	/**
	 * 付费统计信息发送
	 * @param playerCode
	 * @param info
	 */
	static public void stat_支付统计(String playerCode, OrderInfo info) {
		MqMsg_Pay msg = new MqMsg_Pay();
		msg.setCode(playerCode);
		msg.setGoodsid(info.getGoodsid());
		msg.setMyorderid(info.getMyorderid());
		msg.setPayamount(info.getPayamount());
		msg.setReason(CCReason.用户购买);
		msg.setServerid(info.getServerid());
		msg.setTime(info.getUpDateTime().toDate());

		MqMessage.sendMessage(msg);
	}
	
	/**
	 * 新手引导
	 * @param playerBean
	 */
	static public void stat_新手引导(PlayerBean playerBean) {
		String guide = playerBean.getGuide();
		if(guide == null) {
			return;
		}
		
		JSONObject guidejson = JSON.parseObject(guide);
		int guideid = guidejson.getIntValue("guildid");
		
		MqMsg_Guide msg = new MqMsg_Guide();
		msg.setCode(playerBean.getCode());
		msg.setUid(playerBean.getUid());
		msg.setTime(new Date());
		msg.setGuideid(guideid);

		MqMessage.sendMessage(msg);
	}


	/**
	 *
	 * @param playerCode
	 */
	static public void stat_用户登录状态更新(String playerCode) {
		PlayerBean bean = PlayerBean.get(playerCode);
		MqMsg_UpdateTime msg = new MqMsg_UpdateTime();
		msg.setCode(playerCode);
		msg.setServerid(bean.getServerId());
		msg.setTime(new Date());
		msg.setUid(bean.getUid());
		MqMessage.sendMessage(msg);
	}

	/**
	 *
	 */
	static public void stat_神兽挑战记录(String therionName,int gameType,String playerCodes,String ranks,String values) {
		MqMsg_TherionRank msg = new MqMsg_TherionRank();
		msg.setTherionName(therionName);
		msg.setGameType(gameType);
		msg.setPlayerCodes(playerCodes);
		msg.setRanks(ranks);
		msg.setValues(values);
		msg.setTime(new Date());
		MqMessage.sendMessage(msg);
	}

	/**
	 *
	 */
	static public void stat_神兽奖励发放记录(String therionName,int gameType,String playerCodes,String ranks) {
		MqMsg_TherionReward msg = new MqMsg_TherionReward();
		msg.setTherionName(therionName);
		msg.setGameType(gameType);
		msg.setPlayerCodes(playerCodes);
		msg.setRanks(ranks);
		msg.setTime(new Date());
		MqMessage.sendMessage(msg);
	}
}
